import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/cupertino.dart' hide Action;
import 'package:flutter/material.dart' hide Action;
import 'package:rflutter_alert/rflutter_alert.dart';
import 'package:www.znpigai.teacher/utils/http.dart';
import 'package:www.znpigai.teacher/utils/progress_dialog.dart';
import 'package:www.znpigai.teacher/utils/util.dart';

import 'action.dart';
import 'state.dart';

Effect<StudentState> buildEffect() {
  return combineEffects(<Object, Effect<StudentState>>{
    StudentAction.onClick: _onClick,
  });
}

void _onClick(Action action, Context<StudentState> ctx) async {
  String id = action.payload;
  String password = '';
  bool ok = await Alert(
      context: ctx.context,
      title: '修改学生密码',
      content: Column(
        children: <Widget>[
          TextField(
            obscureText: true,
            decoration: InputDecoration(
              labelText: '新密码',
            ),
            onChanged: (input) => password = input,
          ),
        ],
      ),
      buttons: [
        DialogButton(
          onPressed: () => Navigator.pop(ctx.context, false),
          child: Text(
            "取消",
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
        ),
        DialogButton(
          color: Colors.blue,
          onPressed: () => Navigator.pop(ctx.context, true),
          child: Text(
            "修改",
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
        )
      ]).show();

  if (ok == true) {
    if (password.isEmpty) {
      Util.alert('密码不能为空', ctx.context);
      return;
    }

    // 修改密码
    var pr = new ProgressDialog(ctx.context)..setMessage('请等待...');
    pr.show();
    HttpResult result = await Http.post(
        '/correct/teacher/api/class/student/upwd.mvc',
        data: {'id': id, 'pwd': password});
    pr.hide();

    if (result.success) {
      Util.alert('密码修改成功', ctx.context);
    } else {
      Util.alertFail(result, ctx.context);
    }
  }
}
